﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Super.Hmi.Assets.CustomControl
{
    /// <summary>
    /// PageControl.xaml 的交互逻辑
    /// </summary>
    public partial class PageControl : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        public int TotalCount
        {
            get { return (int)GetValue(TotalCountProperty); }
            set { SetValue(TotalCountProperty, value); }
        }
        public static readonly DependencyProperty TotalCountProperty =
            DependencyProperty.Register("TotalCount", typeof(int), typeof(PageControl), new PropertyMetadata(200));

        public PageSizeType PageSize
        {
            get { return (PageSizeType)GetValue(PageSizeProperty); }
            set { SetValue(PageSizeProperty, value); }
        }
        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register("PageSize", typeof(PageSizeType), typeof(PageControl), new PropertyMetadata(PageSizeType.二十, new PropertyChangedCallback(OnPageSizeChanged)));

        public ICommand PageUpdatedCommand
        {
            get { return (ICommand)GetValue(PageUpdatedCommandProperty); }
            set { SetValue(PageUpdatedCommandProperty, value); }
        }
        // 页码更新事件
        public static readonly DependencyProperty PageUpdatedCommandProperty =
            DependencyProperty.Register("PageUpdatedCommand", typeof(ICommand), typeof(PageControl), new PropertyMetadata(null));

        private static void OnPageSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (PageControl)d;
            // 临时移除事件避免循环
            control.ComBoxPageSize.SelectionChanged -= control.OnComboBoxSelectionChanged;
            control.ComBoxPageSize.SelectedIndex = (int)e.NewValue switch
            {
                10 => 0,
                15 => 1,
                20 => 2,
                25 => 3,
                30 => 4,
                _ => 2
            };
            control.ComBoxPageSize.SelectionChanged += control.OnComboBoxSelectionChanged;
            control.PageSizeNum = (int)e.NewValue;
        }
        private int _pageSizeNum = 20;
        public int PageSizeNum
        {
            get { return _pageSizeNum; }
            set
            {
                _pageSizeNum = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PageSizeNum"));
            }
        }

        public int PageIndex
        {
            get { return (int)GetValue(PageIndexProperty); }
            set { SetValue(PageIndexProperty, value); }
        }
        public static readonly DependencyProperty PageIndexProperty =
            DependencyProperty.Register("PageIndex", typeof(int), typeof(PageControl), new PropertyMetadata(1));

        public int PageCount
        {
            get { return (int)GetValue(PageCountProperty); }
            set { SetValue(PageCountProperty, value); }
        }
        public static readonly DependencyProperty PageCountProperty =
            DependencyProperty.Register("PageCount", typeof(int), typeof(PageControl), new PropertyMetadata(10));

        public PageControl()
        {
            InitializeComponent();
            ComBoxPageSize.SelectionChanged += OnComboBoxSelectionChanged;
        }

        private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PageSize = ComBoxPageSize.SelectedIndex switch
            {
                0 => PageSizeType.十,
                1 => PageSizeType.十五,
                2 => PageSizeType.二十,
                3 => PageSizeType.二十五,
                4 => PageSizeType.三十,
                _ => PageSizeType.二十
            };
        }

        public enum PageSizeType
        {
            十 = 10,
            十五 = 15,
            二十 = 20,
            二十五 = 25,
            三十 = 30,
        }
    }
}
